<template>
  <div>
    <div class="top">
      <div style="float: right;margin-right: 10px;">
        <el-input
          v-model="kwInput"
          placeholder="输入交易号或者事件ID 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
      </div>
      <div style="float: right;margin-right: 10px;">
        <el-select v-model="finished" placeholder="状态" @change="handleSearch">
          <el-option label="不过滤" value="" />
          <el-option label="未结束" value="false" />
          <el-option label="已经结束" value="true" />
        </el-select>
      </div>

      <div style="clear: both" />
    </div>

    <el-table
      v-loading.fullscreen.lock="listLoading"
      :data="data"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="handleSort"
    >
      <el-table-column align="center" width="180" prop="case_id" label="事件编号" />
      <el-table-column align="center" width="180" prop="transaction_id" label="交易编号" />
      <el-table-column align="center" width="180" prop="account_email" label="paypal账号" />
      <el-table-column align="center" label="最近邮件" prop="last_subject">
        <template slot-scope="scope">
          <span>{{ scope.row.last_subject }}</span>
          <el-popover
            placement="right"
            width="60%"
            trigger="hover"
          >
            <div>
              <h2>{{ scope.row.last_subject }}</h2>
              <el-tag>To:{{ scope.row.mail_to }}</el-tag>
              <div style="margin-top: 10px" v-html="scope.row.body" />
            </div>
            <i slot="reference" style="float: right" class="el-icon-view" title="查看详情" />
          </el-popover>
          <!--          <span @click="handleDrawer(scope.row)">{{scope.row.subject}}</span>-->
        </template>
      </el-table-column>
      <el-table-column align="center" width="280" prop="domain" label="订单网站" />
      <el-table-column align="center" width="120" prop="psorder_id" label="系统单号">
        <template slot-scope="scope">
          <span title="查询订单信息" @click="handleSearchOrderInfo(scope.row.reference)">{{ scope.row.psorder_id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" width="120" prop="state_name" label="当前状态" />
      <el-table-column align="center" width="100" prop="finished" label="是否结束">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="点击调整状态" placement="bottom-start">
            <el-switch v-model="scope.row.finished" @change="handleModify(scope.row)" />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column align="center" width="120" prop="update_at" label="修改时间" />
      <!--      <el-table-column align="center" label="操作" width="200">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>-->
      <!--          <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

    </el-table>
    <el-drawer
      :wrapper-closable="false"
      title="邮件内容"
      append-to-body
      :visible.sync="drawer"
      size="50%"
    >
      <div>
        <h2>{{ currentrow.subject }}</h2>
        <el-tag>from:{{ currentrow.mail_from }}</el-tag>
        <el-tag>To:{{ currentrow.mail_to }}</el-tag>
        <div style="margin-top: 10px" v-html="currentrow.body" />
      </div>
    </el-drawer>
    <el-pagination
      v-if="data.length"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10,20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />

    <el-dialog
      append-to-body
      :title="temp.subject"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="120px">

        <el-form-item v-if="action !== 'delete'" label="邮箱账号">
          <el-input v-model="temp.subject" />
        </el-form-item>

        <el-form-item v-if="action !== 'delete'" label="邮件内容">
          <div v-html="temp.body" />
        </el-form-item>

        <el-form-item v-if="action !== 'delete'" label="from">
          <el-input v-model="temp.mail_from" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="to">
          <el-input v-model="temp.mail_to" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <orderinfo :keyword="orderinfokeyword" :orderinfo-visible="orderinfoVisible" @closeDialog="orderinfoVisible=false" />
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/eco/paypalcase'
import qs from 'qs'
import Orderinfo from '../orders/element/orderinfo'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Paypalcase',
  components: { Orderinfo },
  data() {
    return {
      orderinfoVisible: false,
      orderinfokeyword: '',
      drawer: false,
      data: [],
      listLoading: false,
      kwInput: '',
      finished: 'false',
      order_by: 'create_at',
      order_desc: 1,
      // pagination
      pagesize: 20,
      page: 1,
      total: 0,
      currentrow: {
        subject: '',
        body: '',
        mail_from: '',
        mail_to: ''
      },
      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        subject: '',
        mail_to: '',
        mail_from: '',
        body: '',
        id: ''
      },
      width: '',
      projects: [],
      innerDialogVisible: false
    }
  },
  watch: {
    webVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleSearchOrderInfo(keyword) {
      this.orderinfokeyword = keyword
      this.orderinfoVisible = true
      console.log(keyword)
    },
    handleDrawer(row) {
      this.currentrow = row
      this.drawer = true
    },
    resetTemp() {
      this.temp = {
        account: '',
        password: 'Euc@16IumGg',
        smtp_host: 'smtp.zoho.com',
        smtp_port: '465',
        project_id: ''
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        finished: this.finished,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleModify(row) {
      Put(row.id, {
        finished: row.finished
      }).then((response) => {
        this.notify('更新成功')
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.domain}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改站点${row.domain}`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.project_id === '') {
            alert('必须选择项目')
            return
          }
          if (!_data.account.length) {
            alert('必须填写邮箱账号')
            return
          }
          if (!_data.password.length) {
            alert('必须填写密码')
            return
          }
          if (!_data.smtp_host.length) {
            alert('必须填写smtp_host')
            return
          }
          Post(_data
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            account: _data.account,
            smtp_host: _data.smtp_host,
            smtp_port: _data.smtp_port,
            password: _data.password,
            project_id: _data.project_id
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeWebDialog', false)
    }
  }
}
</script>
